package com.qdxwx.manage.controller.colleges;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.colleges.CollegesService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.colleges.Colleges;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 合作院校Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/colleges")
public class CollegesController extends BaseController {

    @Autowired
    private CollegesService collegesService;

    @Permissions("colleges:list")
    @GetMapping("/list.html")
    public String list() {
        return "colleges/colleges-list";
    }

    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        String searchKey = TKString.trim(request.getParameter("searchKey"));
        if (TKString.isNotEmpty(searchKey) && TKConvert.toInteger(searchKey) > 0) {
            Colleges colleges = collegesService.get(TKConvert.toInteger(searchKey));
            if (colleges == null) return TableDataInfo.error("找不到指定id的院校");
            List<Colleges> list = new ArrayList<>();
            list.add(colleges);
            return TableDataInfo.success(list, 1);
        }
        Colleges colleges = new Colleges();
        if(TKString.isMobile(searchKey)){
            colleges.setMobile(searchKey);
        }else {
            colleges.setName(searchKey);
        }
        colleges.setStatus(TKConvert.toInteger(request.getParameter("status"),null));
        List<Colleges> list = collegesService.getList(colleges, PageRequest.form(request));
        int count = collegesService.count(colleges);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(count);
        return rspData;
    }
}
